<?php
/**
 * Created by PhpStorm.
 * User: worry
 * Date: 2018/2/8
 * Time: 17:02
 */

namespace common\services;

use common\models\Information;
use common\models\UploadForm;
use Yii;
use yii\helpers\ArrayHelper;
use yii\web\UploadedFile;

class InformationService extends BasicService
{
    /**
     * 保存之前的数据操作
     * @param $data
     * @param int $mode
     * @param array $oldData
     * @return array
     */
    public function beforeSave(&$data, $mode = self::CREATE, $oldData = [])
    {
        $data['entry_time'] = strtotime($data['entry_time']) ?: 0;
        $data['add_time'] = time();
        $data['credit_info'] = json_encode([
            ['bank' => $data['card1'], 'limit' => $data['card1_limit']],
            ['bank' => $data['card2'], 'limit' => $data['card2_limit']],
            ['bank' => $data['card3'], 'limit' => $data['card3_limit']],
        ], JSON_UNESCAPED_UNICODE);
        return $this->returnMsg('数据处理成功', true);
    }

    /**
     * 导出数据
     * @return string
     */
    public function export($map)
    {
        $result = '';
        $model = Information::find();
        if($map) {
            $model = $model->where($map);
        }
        $list = $model->all();
        $title = [
            'name' => '姓名',
            'id_no' => '身份证号码',
            'mobile' => '手机号',
            'is_marry' => '婚姻状况',
            'mobile_pass' => '手机号服务密码',
            'animal_zodiac' => '属相',
            'education_background' => '学历',
            'home_address' => '住宅地址',
            'company_name' => '公司地址',
            'company_address' => '公司地址',
            'company_tel' => '公司电话',
            'entry_time' => '入学时间',
            'credit_card_1' => '信用卡1',
            'credit_limit_1' => '额度',
            'credit_card_2' => '信用卡2',
            'credit_limit_2' => '额度',
            'credit_card_3' => '信用卡3',
            'credit_limit_3' => '额度',
            'credit_email' => '信用卡账单邮箱',
            'credit_email_pass' => '信用卡邮箱密码',
            'zhima_score' => '芝麻分',
            'social_security' => '社保账户',
            'provident_fund' => '公积金账号',
            'qq' => 'qq号',
            'wechat' => '微信号',
            'parents_name' => '父母姓名',
            'parents_mobile' => '父母手机',
            'spouse_name' => '配偶',
            'spouse_mobile' => '配偶手机',
            'friend_name' => '朋友姓名',
            'friend_mobile' => '朋友手机',
            'colleague_name' => '同事姓名',
            'colleague_mobile' => '同事手机',
            'bank_name' => '收款银行名称',
            'bank_no' => '收款银行卡号',
            'add_time' => '添加时间',
        ];
        $result .= join(',', array_map(function ($value) {
                return "\"{$value}\"";
            }, array_values($title))) . "\r\n";
        foreach ($list as $item) {
            /** @var Information $item */
            $credit_info = json_decode($item->credit_info, true);
            $tmp = [];
            foreach (array_keys($title) as $key) {
                $tmp[$key] = isset($item->$key) ? $item->$key : '';
            }
            $tmp['credit_card_1'] = ArrayHelper::getValue(isset($credit_info[0]) ? $credit_info[0] : [], 'bank');
            $tmp['credit_limit_1'] = ArrayHelper::getValue(isset($credit_info[0]) ? $credit_info[0] : [], 'limit');
            $tmp['credit_card_2'] = ArrayHelper::getValue(isset($credit_info[1]) ? $credit_info[1] : [], 'bank');
            $tmp['credit_limit_2'] = ArrayHelper::getValue(isset($credit_info[1]) ? $credit_info[1] : [], 'limit');
            $tmp['credit_card_3'] = ArrayHelper::getValue(isset($credit_info[2]) ? $credit_info[2] : [], 'bank');
            $tmp['credit_limit_3'] = ArrayHelper::getValue(isset($credit_info[2]) ? $credit_info[2] : [], 'limit');
            $tmp['entry_time'] = date('Y-m-d', $item->entry_time);
            $tmp['add_time'] = date('Y-m-d', $item->add_time);
            $array = [];
            foreach ($title as $key => $value) {
                if (!isset($tmp[$key])){
                    $array[$key] = "\"\"";
                }else if(is_numeric($tmp[$key])) {
                    $array[$key] = "\"\t{$tmp[$key]}\t\"";
                } else {
                    $array[$key] = "\"{$tmp[$key]}\"";
                }

            }
            $result .= join(',', $array) . "\r\n";
        }
        return $result;
    }
}